<?php
namespace model;

class Login{
			
	private static $salt = "salt";
	private static $loggedIn = 'loggedIn';
	private static $Admin = 'Admin';
	private static $Password = 'Password';
	private static $session_security = 'session_security';
	private static $webbläsare = 'webbläsare';
	
	/*
	 * checks cookies
	 * return string (message)
	 */
	public function checkCookies($cookieUsername, $cookiePassword){
		
		if ($this->checkTimeOnCookie() == FALSE){
			$message = 8;
			return $message;
		}
		if($cookieUsername == self::$Admin && $cookiePassword == md5(self::$Password . self::$salt)){
			$message = 9;
			$this->createSession();
			
		}else{
			$message = 10;
		}
		return $message;
	}
	
	/*
	 * try to log in
	 * return string (message)
	 */
	public function tryLogIn($username, $password){
		try{
				if ($username == self::$Admin && $password == self::$Password ){
						$message = 1;
					$this->createSession();
					return $message;
				}else{
					if(empty($username)){
						$message = 3;
					}
					else if(empty($password)){
						$message = 4;
					}
					else{
						$message = 5;
					}
					return $message;
				}
		}catch(exception $e){echo"Something wrong in model";}
	}
	
	//checks if sesseion logged in is set.
	public function checkIfLoggedIn(){
		try{
				if(isset($_SESSION[self::$loggedIn])){
					return TRUE;
				}else{
					return FALSE;
				}
			}
		catch(Exception $e){
			echo "Something wrong in model";
		}
	}
	
	//logs out || destroys sesseions and cookies
	//return string (message)
	public function logOut(){
			
			unset($_SESSION[self::$loggedIn]);
			session_destroy();
			$message = 6;
		    return $message;
		}

	/*
	 * checks time on cookie
	 */
	private function checkTimeOnCookie(){
		$cookieEndTime = file_get_contents("endtime.txt");
		try{
			if(time() < $cookieEndTime){
				return true;
			}else if (time() > $cookieEndTime){
				return false;
			}
		}catch(exception $e){
			echo "Something wrong in model";
		}
		
	}

	/*
	 * create session 
	 */
	private function createSession(){
		$_SESSION[self::$loggedIn] = true;
		
		$_SESSION[self::$session_security] = array();
		$_SESSION[self::$session_security][self::$webbläsare] = $this->getUserAgent();
	}
	
	/*
	 * checks session
	 */
	public function checkSession(){
		if($_SESSION[self::$session_security][self::$webbläsare] == $this->getUserAgent()){
			return $message = 0;
		}else {
			unset($_SESSION[self::$loggedIn]);
			session_destroy();
			return $message = 7;
		}		
	}
	
	// Magic happens in this function to find out the users browser
	//http://stackoverflow.com/questions/9693574/user-agent-extract-os-and-browser-from-string-php
	public static function getUserAgent(){
    static $agent = null;

    if ( empty($agent) ) {
        $agent = $_SERVER['HTTP_USER_AGENT'];

        if ( stripos($agent, 'Firefox') !== false ) {
            $agent = 'firefox';
        } elseif ( stripos($agent, 'MSIE') !== false ) {
            $agent = 'ie';
        } elseif ( stripos($agent, 'iPad') !== false ) {
            $agent = 'ipad';
        } elseif ( stripos($agent, 'Android') !== false ) {
            $agent = 'android';
        } elseif ( stripos($agent, 'Chrome') !== false ) {
            $agent = 'chrome';
        } elseif ( stripos($agent, 'Safari') !== false ) {
            $agent = 'safari';
        } elseif ( stripos($agent, 'AIR') !== false ) {
            $agent = 'air';
        } elseif ( stripos($agent, 'Fluid') !== false ) {
            $agent = 'fluid';
        }

    }
    return $agent;
	}
}





